<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="./vue.js"></script>
    <style>
        .active {
            color: aquamarine;
            text-decoration: line-through;
        }
    </style>
</head>

<body>
    <div id="wrap">
        <info-title></info-title>
        <info-main></info-main>
    </div>

    <!-- 子组件1 -->
    <template id="title">
        <div>
            <input type="text" v-model="str"> <button @click="addinfo"> add </button>
        </div>
    </template>
    <!-- 子组件2 -->
    <template id="main">
        <div>
            <div v-for="(msg,i) in infolist">
                <span :class="msg.status ? 'active' : '' ">{{msg.message}}</span>
                <button @click="changeStatus(i)" v-if="msg.status">已完成</button>
                <button @click="changeStatus(i)" v-else>未完成</button>
                <button @click="del(i)">删除</button>
            </div>
        </div>
    </template>
</body>
<script>
    let angle = new Vue({})

    Vue.component('infoTitle', {
        template: '#title',
        data() {
            return {
                str: ''
            }
        },
        methods: {
            addinfo() {
                if (this.str) {
                    item = this.str
                    angle.$emit('pushinfo', item)
                    this.str = ''
                }
            }
        }
    })

    Vue.component('infoMain', {
        template: '#main',
        data() {
            return {
                infolist: [{ message: '开始今天的计划咯', status: false }],
            }
        },
        created() {
            angle.$on('pushinfo', (item) => {
                this.infolist.push({ message: item, status: false })
            })
        },
        methods: {
            changeStatus(i) {
                this.infolist[i].status = !this.infolist[i].status
            },
            del(i) {
                this.infolist.splice(i, 1)
            }
        }
    })

    new Vue({
        el: '#wrap',
    })





</script>

</html>